package com.yaoxun.console.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.yaoxun.console.constant.CacheConstant;
import com.yaoxun.console.xo.entity.LoginUser;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class SuperUserService {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Autowired
	private JsonService jsonService;
	
	private static final String LOGIN_NAME = "admin";
	
	private static final String PASSWORD = "admin123";
	
	public void init() {
		
		log.info("ready to create SuperUser");
		
		String value = (String) stringRedisTemplate.opsForHash().get(CacheConstant.LOGIN_USERS, LOGIN_NAME);
		if(value != null) {
			log.info("SuperUser【{}】 already exists!", LOGIN_NAME);
			return ;
		}
		
		LoginUser loginUser = new LoginUser();
		loginUser.setLoginName(LOGIN_NAME)
			     .setPassword(PASSWORD)
			     .setCreateTime(new Date());
		
		String json = jsonService.toJsonString(loginUser);
		
		stringRedisTemplate.opsForHash().put(CacheConstant.LOGIN_USERS, LOGIN_NAME, json);
		
		log.info("SuperUser【{}】create success!", LOGIN_NAME);
		
	}
	
}
